<?php
	class CodeTransfer{
		var $codefile;
		var $codename;
		var $chrheads=array();
		var $chrbodys=array();
		function CodeTransfer($code){
			$this->codename=$code;
			switch($code){
				case "b2g":		//繁轉簡
					$this->codefile="big5_gb.cod";
					break;
				case "b2j":		//繁轉日
					$this->codefile="big5_jp.cod";
					break;
				case "g2b":		//簡轉繁
					$this->codefile="gb_big5.cod";
					break;
				case "b2u":		//繁轉uncode
					$this->codefile="big5_uni.cod";
					break;
				case "g2j":		//簡轉日
					$this->codefile="gb_jp.cod";
					break;
			}
			//echo $this->codefile."<br>";
			$sql="select paramvalue from s_param where ";
			$sql.="paramname='IF_Codetrans_codeloc'";
			$mytb=mysql_query($sql);
			if ($mytb){
				$myrow=mysql_fetch_object($mytb);
				$codepath=$myrow->paramvalue;
			} else {
				$codepath='';
			}
			$myfd=fopen($codepath.$this->codefile,"r");
			while (!feof($myfd)){
				$this->chrheads[]=fgetc($myfd);
				$this->chrbodys[]=fgetc($myfd);
			}
		}

		function Transfer($myString){
			$lens=strlen($myString);
			for ($i=0;$i<$lens;$i++){
				$chrhead=substr($myString,$i,1);
				if (ord($chrhead)<128){
					$newString.=$chrhead;
				} else {
					$i++;
					$chrbody=substr($myString,$i,1);
					$pos=(ord($chrhead)-128)*256+ord($chrbody);
					if ($this->codename=="b2u"){
						$code=$this->dechex2(ord($this->chrheads[$pos]));
						$code.=$this->dechex2(ord($this->chrbodys[$pos]));
						//echo $code." :thisline->";
						$newString.="&#".hexdec($code).";";
					} else {
						$newString.=$this->chrheads[$pos].$this->chrbodys[$pos];
					}
				}
			}
			return $newString;
		}

		function Show(){
			for ($i=0;$i<count($this->chrheads);$i++){
				echo ord($this->chrheads[$i]);
				echo " ";
				echo ord($this->chrbodys[$i]);
				echo " ";
			}
		}

		function dechex2($data){
			$ndata=dechex($data);
			if (strlen($ndata)==1){
				$ndata="0".$ndata;
			}
			return $ndata;
		}

		function Transimg ($myFileName) {
			$str = "select paramvalue from s_param where paramname='IF_Codetrans_fontimg'";
			$result = mysql_query($str);
			echo $str;
			echo mysql_error();
			list($font_img) = mysql_fetch_row($result);
			echo "font_img=".$font_img;
			$myFile=fopen($myFileName,"r");
			$myContents = fread($myFile,filesize($myFileName));
			fclose($myFile);
			$lens=strlen($myContents);
			for ($i=0;$i<$lens;$i++){
				$chrhead=substr($myContents,$i,1);
				if (ord($chrhead)<128){
					$newString.=$chrhead;
				} else {
					$i++;
					$chrbody=substr($myContents,$i,1);
					$pos=(ord($chrhead)-128)*256+ord($chrbody);
					if ($this->codename=="b2u"){
						$code=$this->dechex2(ord($this->chrheads[$pos]));
						$code.=$this->dechex2(ord($this->chrbodys[$pos]));
						$myString = hexdec($code);
						$newString.= '<img src="'.$font_img.$myString.'.png" border="0"> ';
					} else {
						$newString.=$this->chrheads[$pos].$this->chrbodys[$pos];
					}
				}
			}
			$myFileName1 = "/home/sting/public_html/oh.html";
			$myFiletemp=fopen($myFileName1,"w");
			fputs($myFiletemp,$newString,strlen($newString));
			fclose($myFiletemp);
		}
	}
?>